package cn.hqc.msg.service.impl;


import cn.hqc.basic.jwt.UserInfo;
import cn.hqc.basic.service.impl.BaseServiceImpl;
import cn.hqc.basic.utils.*;
import cn.hqc.msg.domain.SearchMasterMsg;
import cn.hqc.msg.domain.SearchMasterMsgAuditLog;
import cn.hqc.msg.dto.AcceptDto;
import cn.hqc.msg.dto.SearchMsgDto;
import cn.hqc.msg.mapper.SearchMasterMsgAuditLogMapper;
import cn.hqc.msg.mapper.SearchMasterMsgMapper;
import cn.hqc.msg.service.ISearchMasterMsgService;
import cn.hqc.order.domain.OrderPetAcquisition;
import cn.hqc.order.mapper.OrderPetAcquisitionMapper;
import cn.hqc.org.domain.Shop;
import cn.hqc.org.mapper.ShopMapper;
import cn.hqc.pet.domain.Pet;
import cn.hqc.pet.mapper.PetMapper;
import cn.hqc.user.domain.Logininfo;
import cn.hqc.user.domain.User;
import cn.hqc.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-05-14
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper logMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper msgMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderMapper;

    /**
     * 发布寻主消息
     *
     * @param dto
     */
    @Override
    public void publish(SearchMsgDto dto) {
        // 参数校验
        // 保存数据
        SearchMasterMsg msg = new SearchMasterMsg();
        BeanUtils.copyProperties(dto, msg);
        msg.setState(0);
        UserInfo userInfo = ThreadLocalUtil.get();
        Logininfo logininfo = userInfo.getLogininfo();
        // 根据logininfoid查询userid
        User user = userMapper.findByLogininfoId(logininfo.getId());
        msg.setUserId(user.getId());
        this.add(msg);
        // 百度ai审核 审核名称
        if (!BaiduAuditUtils.TextCensor(dto.getName())) {
            // 添加日志
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(msg.getId());
            log.setAuditId(-1L);
            log.setAuditTime(new Date());
            log.setState(0);
            log.setNote("名称违规：" + dto.getName());
            logMapper.add(log);

            // 消息状态改成 审核失败
            msg.setState(2);
            this.update(msg);
            return;
        }
        // 寻找50公里以内的店铺
        List<Shop> shops = shopMapper.findAll();
        String address = dto.getAddress();
        Point point = DistanceUtil.getPoint(address);
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        // 找到的话 将寻主消息分配给店铺
        if (null != nearestShop) {
            msg.setShopId(nearestShop.getId());
            msg.setState(1);
            this.update(msg);
        }
    }

    /**
     * 拒单
     *
     * @param msgId
     */
    @Override
    public void reject(Long msgId) {
        // 将当前消息的id置空
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(msgId);
        msg.setShopId(null);
        msgMapper.updateByMsg(msg);
    }

    /**
     * 接单
     *
     * @param dto
     */
    @Override
    public void accept(AcceptDto dto) {
        // 参数校验
        // 根据msgId 将msg数据查询出来
        SearchMasterMsg msg = msgMapper.findById(dto.getMsgId());
        // 修改msg状态
        msg.setState(3);
        this.update(msg);
        // 生成宠物信息
        Pet pet = this.getPet(msg);
        petMapper.add(pet);
        // 生成收购单
        OrderPetAcquisition order = getOrder(dto, msg, pet);
        orderMapper.add(order);
    }

    private OrderPetAcquisition getOrder(AcceptDto dto
            , SearchMasterMsg msg, Pet pet) {
        OrderPetAcquisition order = new OrderPetAcquisition();
        // 订单编号 日期 不可能重复 uuid
        // redis：incr 年月日时分秒毫秒 + 自增(5) +随机(4) 230515111730123 00001 3212
        // 雪花算法
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(msg.getUserId()));
        order.setDigest(dto.getNote());
        order.setLastConfirmTime(new Date(System.currentTimeMillis() + 2 * 24 * 60 * 60 * 1000));
        order.setState(0);
        order.setPrice(BigDecimal.valueOf(msg.getPrice()));
        order.setPayType(0);
        // 支付编号
        order.setPaySn(CodeGenerateUtils.generateUnionPaySn());
        order.setPetId(pet.getId());
        order.setUserId(msg.getUserId());
        order.setShopId(msg.getShopId());
        order.setAddress(msg.getAddress());
        order.setEmpId(dto.getHandler());
        order.setMsgId(msg.getId());
        return order;

    }

    private Pet getPet(SearchMasterMsg msg) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(BigDecimal.valueOf(msg.getPrice()));
        pet.setState(0);
        pet.setTypeId(msg.getPetType());
        pet.setCreatetime(new Date());
        pet.setShopId(msg.getShopId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;

    }
}
